/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
// pdirbwr.hpp - PegDirectoryBrowser dialog.
//
// A bit of a clone for the Win32 API SHBrowseForFolder functionality
// for use on Unix systems.
//
// This class may be optionally included in the standard PEG library if
// needed, but is originally intended to be used along with the PEG
// utilities (WindowBuilder, ImageConvert and FontCapture).
//
// The PegDirectoryTree can be used as a general purpose directory tree
// contained within other PEG window classes, separate from the
// PegDirectoryBrowser dialog. The PegDirectoryTree itself does the
// work of traversing and displaying the directory hierarchy of the
// given host. 
//
// Author: Jim DeLisle
//
// Copyright (c) 1998-2002 Swell Software 
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software
// is strictly prohibited.
//
// Notes:
//
//    This class is intended for use on Unix style OS's only. For Win32
//  development platforms, use the standard, but obscure, Win32 API
//  SHBrowseForFolder dialog box.
//
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#ifndef _PDIRBWR_HPP_
#define _PDIRBWR_HPP_

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
class PegDirectoryTree : public PegTreeView
{
    public:
        PegDirectoryTree(const PegRect &Rect, PEGUINT Style,
                         const PEGUINT StringId = 0,
                         const char *pRoot = NULL);
        virtual ~PegDirectoryTree();

        // returns the len of the string
        PEGUINT BuildDirString();

        // Accessors
        char* GetDirString() const { return mpDirBuff; }
        PEGUINT GetDirStringLen() const { return mDirBuffLen; }
        char* GetRootDir() const { return mpRootDir; }

        // returns the number of dirs encountered
        PEGULONG Refresh();    

        // Mutators
        void SetRootDir(const char* pRoot, PEGBOOL Update = TRUE);

        // qsort callback
        static int SortDirList(const void*, const void*);

        virtual void ToggleBranch(PegTreeNode* pNode);

    protected:
        // returns the number of dirs added
        PEGULONG AddChildDirs(PegTreeNode* pParent, const char* pStartDir,
                              PEGUINT RecurseLevel = 1, 
                              PEGBOOL CheckOnly = FALSE);
        void AddDummyNode(PegTreeNode* pParent);
        PEGUINT BuildDirString(PegTreeNode* pStart);
        PegTreeNode* HasDummyNode(PegTreeNode* pParent);
        PEGBOOL RemoveDummyNode(PegTreeNode* pParent);

        char* mpDirBuff;
        char* mpRootDir;

        PEGUINT mDirBuffLen;
};

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
class PegDirectoryBrowser : public PegDialog
{
    public:
        PegDirectoryBrowser(const PegRect& Rect, const PEGUINT TitleId,
                            const PEGUINT CommentId, 
                            PEGCHAR *pDirBuffer,
                            const PEGUINT BuffSize);

        PegDirectoryBrowser(const PEGUINT TitleId,
                            const PEGUINT CommentId,
                            PEGCHAR *pDirBuffer, const PEGUINT BuffSize);

        virtual ~PegDirectoryBrowser();

        virtual PEGINT Message(const PegMessage& Mesg);
        void Resize(PegRect NewSize);
        void SetRootDir(const PEGCHAR *pRoot, PEGBOOL Refresh = TRUE);

    private:
        void GetCancelRect(PegRect &Rect);
        void GetCommentRect(PegRect &Rect);
        void GetOKRect(PegRect &Rect);
        void GetTreeRect(PegRect &Rect);
        void InitMembers(const PEGUINT CommentId, PEGCHAR *pDirBuffer,
                         const PEGUINT BuffSize);
        
        PegDirectoryTree *mpDirTree;
        PegPrompt *mpComment;
        PEGCHAR  *mpDirBuff;
        PEGUINT  mDirBuffSize;
};

#endif  // _PDIRBWR_HPP_

// End of file

